﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AMicroblogAPI;
using AMicroblogAPI.Common;

namespace weibo
{
    public partial class Login : Form
    {
        public static OAuthAccessToken accessToken;

        public Login()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                checkLogin(tbUsername.Text.Trim(), tbPassword.Text);
                MainWindow mw = new MainWindow();
                this.Hide();
                mw.ShowDialog();
                AMicroblog.Logout();
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message);
            }
        }

        private void checkLogin(string username, string password)
        {
            if (string.IsNullOrEmpty(username))
            {
                throw new Exception("Invalid user name or password.");
            }
            if (string.IsNullOrEmpty(password))
            {
                throw new Exception("Invalid user name or password.");
            }
            accessToken = AMicroblog.Login(username, password);
        }
    }
}
